package com.hyfd.deal.Flow;

import java.net.SocketTimeoutException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.hyfd.common.GenerateData;
import com.hyfd.common.utils.MapUtils;
import com.hyfd.common.utils.PostUtil;
import com.hyfd.common.utils.Sign;
import com.hyfd.common.utils.ToolDateTime;
import com.hyfd.common.utils.XmlUtils;
import com.hyfd.deal.BaseDeal;

/**
 * Create Date: 2017年8月31日 下午2:45:42
 * 
 * @version: V3.0.0
 * @author: zj
 */
public class ZhongJinFlowDeal implements BaseDeal
{
    Logger log = Logger.getLogger(getClass());
    
    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> deal(Map<String, Object> order)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        int flag = -1;
        
        try
        {
            // 手机号
            String phoneNumber = (String)order.get("phone");
            // 平台订单号
            String curids =
                    ToolDateTime.format(new Date(), "yyyyMMddHHmmssSSS") + phoneNumber + GenerateData.getIntData(9, 2);
            map.put("orderId", curids);
            
            Map<String, Object> channel = (Map<String, Object>)order.get("channel");// 获取通道参数
            String linkUrl = (String)channel.get("link_url");// 充值地址
            String defaultParameter = (String)channel.get("default_parameter");// 默认参数
            Map<String, String> paramMap = XmlUtils.readXmlToMap(defaultParameter);
            String channelId = paramMap.get("channelId"); // 渠道商编号 
            String key = paramMap.get("key");// 密钥
            //String notifyUrl = paramMap.get("notifyUrl");// 订购结果回调地址
            
            String spec = new Double(order.get("value") + "").intValue() + ""; // 流量包大小
            String providerId = order.get("providerId") + ""; //运营商ID
            String productId = getProdId(spec, providerId);
            String payMoney = order.get("fee") + "";
            
            // 组装包体
            JsonObject data = new JsonObject();
            data.addProperty("channel_id", channelId);
    		data.addProperty("channel_no", curids);
    		data.addProperty("mobile", phoneNumber);
    		data.addProperty("product_id", productId);
    		data.addProperty("pay_money", payMoney);
    		data.addProperty("sign", Sign.sign(key,data));// 设置签名
    		String postUrl = linkUrl+"/recharge";
    		String message = data.toString();
    		String retMsg = PostUtil.httpPostWithJSON(postUrl, message);
            
            // 判断是否成功
    		if(retMsg != null){
    			Map<String,String> resultMap = new HashMap<String, String>();
    			resultMap = (Map<String, String>) JSONObject.parse(retMsg);
    			
    			String channelOrderId = resultMap.get("serialNumber");
    			map.put("providerOrderId", channelOrderId);
    			String result = resultMap.get("code");
    			if("0000".equals(result)){// 提交成功
    				flag = 1;
    			}else {//提交失败
					flag = 0;
				}
    			
    		}
        }
        catch (ConnectTimeoutException | SocketTimeoutException e)
        {// 请求、响应超时
            flag = -1;
        }
        catch (Exception e)
        {
            log.error("中金流量出错" + e + MapUtils.toString(order));
        }
        map.put("status", flag);
        return map;
    }
    
    /**
     * 获取产品ID
     * @param spec
     * @param providerId
     * @return
     */
    public static String getProdId(String spec, String providerId)
    {
        if ("0000000001".equals(providerId))
        {
            if (spec.length() == 2)
            {
                spec = "200000" + spec;
            }
            else if (spec.length() == 3)
            {
                spec = "20000" + spec;
            }
            else if (spec.length() == 4)
            {
                spec = "2000000" + spec.substring(0,1);
            }
            else if (spec.length() == 5)
            {
                spec = "200000" + spec.substring(0,2);
            }
        }
        else if ("0000000002".equals(providerId))
        {
            if (spec.length() == 2)
            {
                spec = "100000" + spec;
            }
            else if (spec.length() == 3)
            {
                spec = "10000" + spec;
            }
            else if (spec.length() == 4)
            {
                spec = "1000000" + spec.substring(0,1);
            }
        }
        else if ("0000000003".equals(providerId))
        {
            if (spec.length() == 1)
            {
                spec = "3000000" + spec;
            }
            else if (spec.length() == 2)
            {
                spec = "300000" + spec;
            }
            else if (spec.length() == 3)
            {
                spec = "30000" + spec;
            }
            else if (spec.length() == 4)
            {
                spec = "3000000" + spec.substring(0,1);
            }
        }
        return spec;
    }
    public static void main(String[] args) {
		String spec = "10241";
		String providerid = "0000000001";
		String productId = getProdId(spec, providerid);
		System.out.println("proiductId="+productId);
		
	}
}
